<?php

class FloderController extends ActionController {

    public $layout = '/layouts/common';

    public function actionList() {
        if (Yii::app()->request->isAjaxRequest && Yii::app()->request->isPostRequest) {
            /**
             * 查询分页列表
             */
            $type = Yii::app()->request->getPost('type', '');
            $limit = intval(Yii::app()->request->getPost('limit', 10));
            $page = intval(Yii::app()->request->getPost('page', 1));
            if (!isset(Floder::$type_map[$type])) {
                $this->error('不支持的类型');
            }
            $floder = Floder::model();
            $floder->type = $type;
            $page_data = $floder->getPageList($page, $limit);
            $app_list = Yii::app()->params['wechat']['app'];
            foreach ($page_data['data'] as $k => $v) {
                $app_id = $v['app_id'];
                $v['app_name'] = isset($app_list[$app_id]) ? $app_list[$app_id]['app_name'] : '';
                $page_data['data'][$k] = $v;
            }
            $this->jsonResponse($page_data);
        } else {
            $type = Yii::app()->request->getQuery('type', '');
            if (!isset(Floder::$type_map[$type])) {
                $this->error('不支持的类型');
            }
            $type_name = Floder::$type_map[$type];
            $this->render('list', array('type' => $type, 'type_name' => $type_name));
        }
    }
    /**
     * 添加
     */
    public function actionCreate() {
        $type = Yii::app()->request->getParam('type', '');
        if (!isset(Floder::$type_map[$type])) {
            $this->error('不支持的类型');
        }
        if (Yii::app()->request->isAjaxRequest && Yii::app()->request->isPostRequest) {
            $data = array(
                'name' => Yii::app()->request->getPost('name'),
                'note' => Yii::app()->request->getPost('note'),
                'pid' => Yii::app()->request->getPost('pid', 0),
                'app_id' => Yii::app()->request->getPost('app_id', 0),
                'thumb_sign' => Yii::app()->request->getPost('thumb_sign', ''),
                'type' => $type
            );
            $floder = new Floder();
            $is_success = $floder->create($data);
            if ($is_success === false) {
                $errors = $floder->getErrors();
                $this->error($errors);
            }
            $this->success('添加成功');
        } else {
            $pid = Yii::app()->request->getParam('pid', 0);
            $view_params = array('type' => $type, 'pid' => $pid);
            if (empty($pid)) {
                //一级目录可以选择所属应用
                $app_list = Yii::app()->params['wechat']['app'];
                $view_params['app_list'] = $app_list;
            }
            $this->render('create', $view_params);
        }
    }

    /**
     * 删除
     */
    public function actionDelete() {
        if (Yii::app()->request->isAjaxRequest) {
            $id = Yii::app()->request->getQuery('id');
            if (empty($id)) {
                $this->error('参数非法');
            }
            $floder = Floder::model()->findByPk($id);
            if ($floder === null) {
                $this->error('不存在的记录');
            }
            $is_success = $floder->remove();
            if ($is_success) {
                $this->success('删除成功');
            }
            $this->error('删除失败');
        }
    }

    public function actionBatchDelete() {
        if (Yii::app()->request->isAjaxRequest) {
            $id_list = Yii::app()->request->getPost('id_list');
            if (empty($id_list)) {
                $this->error('参数非法');
            }
            $floder_list = Floder::model()->findAllByPk($id_list);
            if (empty($floder_list)) {
                $this->error('不存在的记录');
            }
            $total = count($floder_list);
            $fail = 0;
            foreach ($floder_list as $floder) {
                $is_success = $floder->remove();
                if (!$is_success) {
                    $fail++;
                }
            }
            $success = $total-$fail;
            $msg = "总共{$total}个,成功{$success}个，失败{$fail}";
            $this->success($msg);
        }
    }

    /**
     * 修改
     */
    public function actionModify() {
        $id = Yii::app()->request->getQuery('id');
        if (empty($id)) {
            $this->error('参数非法');
        }
        $floder = Floder::model()->findByPk($id);
        if ($floder === null) {
            $this->error('记录不存在');
        }
        if (Yii::app()->request->isPostRequest) {
            $name = Yii::app()->request->getPost('name');
            $note = Yii::app()->request->getPost('note', '');
            $thumb_sign = Yii::app()->request->getPost('thumb_sign', '');
            $app_id = Yii::app()->request->getPost('app_id', 0);
            if (empty($name)) {
                $this->error('名称不能为空');
            }
            $floder->name = $name;
            $floder->note = $note;
            $floder->app_id = $app_id;
            if (!empty($thumb_sign)) {
                $floder->thumb_src = $floder->getProcessImg($thumb_sign);
            }
            $is_success = $floder->save();
            if ($is_success) {
                $this->success('编辑成功');
            }
            $this->error('编辑失败');
        } else {
            $view_data = $floder->getAttributes();
            if ($floder->pid == 0) {
                $app_list = Yii::app()->params['wechat']['app'];
                $view_data['app_list'] = $app_list;
            }
            $this->render('modify', $view_data);
        }
    }

    /**
     * 二级目录
    */
    public function actionSubList() {
        $pid = Yii::app()->request->getParam('pid');
        if (empty($pid)) {
            $this->error('参数非法');
        }
        $floder = Floder::model();
        if (Yii::app()->request->isAjaxRequest) {
            $floder->pid = $pid;
            $limit = intval(Yii::app()->request->getPost('limit', 10));
            $page = intval(Yii::app()->request->getPost('page', 1));
            $floder_list = $floder->getPageList($page, $limit);
            $this->jsonResponse($floder_list);
        } else {
            $floder = $floder->findByPk($pid);
            $params = array(
                'type_name' => Floder::$type_map[$floder->type],
                'pid' => $pid,
                'floder' => $floder,
            );
            $this->render('subList', $params);
        }
    }

    /**
     * 目录上传缩略图
    */
    public function actionUploadThumb() {
        $image = Image::model();
        $upload_ret = $image->upload();
        if ($upload_ret === false) {
            $this->error($image->getErrors());
        }
        $this->success('上传成功',$upload_ret);
    }

    /**
     * 生成微信应用菜单
    */
    public function actionMakeWechatMenu() {
        $type = Yii::app()->request->getParam('type');
        if (!in_array($type, array('image','video'))) {
            $this->error('非法的类型');
        }

        $all_floder = Floder::model()->getSubFloder($type);
        if (empty($all_floder['parent'])) {
            $this->error('未创建目录，无法生成菜单');
        }
        $host_info = Yii::app()->request->hostInfo;
        $button = array();
        foreach ($all_floder['parent'] as $parent) {
            $id = $parent->id;
            $sub_button = array();
            if (isset($all_floder['sub'][$id])) {
                foreach ($all_floder['sub'][$id] as $sub) {
                    $sub_button[] = array(
                        'type' => 'view',
                        'name' => $sub->name,
                        'url' => $host_info . $this->createUrl("/wechat/w{$type}/list", array('pid'=>$sub->id))
                    );
                }
            }
            $button['button'][] = array(
                'name' => $parent->name,
                'sub_button' => $sub_button
            );
        }
        try {
            $wechat = new WechatApi($type);
        } catch (CException $e) {
            $this->error($e->getMessage());
        }
        $ret = $wechat->makeMenu($button);
        if ($ret['errcode'] != 0) {
            $this->error($ret['errmsg']);
        }
        $this->success('菜单生成成功');
    }

}